﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Web
{
    public delegate void PageChangeHandler();
    public partial class WebUserControlPager : System.Web.UI.UserControl
    {
        public event PageChangeHandler OnPageChange;
        protected string clientId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            clientId = this.ClientID;
        }

        protected override void OnPreRender(EventArgs e)
        {
            SettingButtnoStatus();
        }

        #region setting status
        private void SetControlStatus(HtmlGenericControl control, HtmlTextWriterStyle htmlTextWriterStyle, string value, bool disabled, string onclickValue, string cssName)
        {
            control.Style.Add(htmlTextWriterStyle, value);
            //control.Disabled = disabled;
            if (onclickValue == null || onclickValue == "")
            {
                control.Attributes.Remove("onclick");
            }
            else
            {
                control.Attributes.Add("onclick", onclickValue);
            }
            control.Attributes.Add("class", cssName);
        }

        private void SettingButtnoStatus()
        {
            if (this.TotalRecord <= 0)
            {

                this.SetControlStatus(btnFirst, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                this.SetControlStatus(btnLast, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                this.SetControlStatus(btnNext, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                this.SetControlStatus(btnPrev, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
            }
            else
            {
                if (CurrentPageIndex == 1 && PageCount == 1)
                {

                    this.SetControlStatus(btnFirst, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                    this.SetControlStatus(btnLast, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                    this.SetControlStatus(btnNext, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                    this.SetControlStatus(btnPrev, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                }
                else if (CurrentPageIndex == 1 && PageCount > 1)
                {
                    this.SetControlStatus(btnFirst, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                    this.SetControlStatus(btnPrev, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                    this.SetControlStatus(btnLast, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('last')", "pgNext");
                    this.SetControlStatus(btnNext, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('next')", "pgNext");
                }
                else if (CurrentPageIndex > 1 && CurrentPageIndex == PageCount)
                {
                    this.SetControlStatus(btnFirst, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('first')", "pgNext");
                    this.SetControlStatus(btnPrev, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('prev')", "pgNext");
                    this.SetControlStatus(btnLast, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                    this.SetControlStatus(btnNext, HtmlTextWriterStyle.Cursor, "auto", true, null, "pgEmpty");
                }
                else if (CurrentPageIndex > 1 && CurrentPageIndex < PageCount)
                {
                    this.SetControlStatus(btnFirst, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('first')", "pgNext");
                    this.SetControlStatus(btnPrev, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('prev')", "pgNext");
                    this.SetControlStatus(btnLast, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('last')", "pgNext");
                    this.SetControlStatus(btnNext, HtmlTextWriterStyle.Cursor, "pointer", false, "GoPage('next')", "pgNext");
                }
            }
        }
        #endregion

        protected void GoPage(object sender, EventArgs e)
        {
            string status = this.hidPageStatus.Value;
            bool tag = false;
            if (this.TotalRecord > 0)
            {
                switch (status)
                {
                    case "first":
                        this.CurrentPageIndex = 1;
                        break;
                    case "next":
                        if (this.CurrentPageIndex < this.PageCount)
                        {
                            this.CurrentPageIndex = this.CurrentPageIndex + 1;
                        }
                        break;
                    case "prev":
                        if (this.CurrentPageIndex > 1)
                        {
                            this.CurrentPageIndex = this.CurrentPageIndex - 1;
                        }
                        break;
                    case "last":
                        this.CurrentPageIndex = this.PageCount;
                        break;

                }
                if (OnPageChange != null)
                {
                    OnPageChange();
                }
            }
        }

        #region property
        public int PageSize
        {
            get
            {
                if (this.ViewState["PageSize"] != null)
                {
                    return int.Parse(this.ViewState["PageSize"].ToString());
                }
                else
                {
                    return 10;
                }
            }
            set
            {
                this.ViewState["PageSize"] = value;
            }
        }
        public int PageCount
        {
            get
            {
                return (this.TotalRecord + PageSize - 1) / PageSize;
            }

        }
        public int TotalRecord
        {
            get
            {
                if (this.ViewState["TotalRecord"] != null)
                {
                    return int.Parse(this.ViewState["TotalRecord"].ToString());
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                this.ViewState["TotalRecord"] = value;
            }
        }
        public int CurrentPageIndex
        {
            get
            {
                if (this.ViewState["CurrentPageIndex"] == null)
                {
                    return 1;
                }
                else
                {
                    int currentPageIndex = int.Parse(this.ViewState["CurrentPageIndex"].ToString());
                    if (currentPageIndex > PageCount)
                    {
                        this.CurrentPageIndex = PageCount;
                        currentPageIndex = PageCount;
                    }
                    return currentPageIndex;
                }
            }
            set
            {
                this.ViewState["CurrentPageIndex"] = value;
            }
        }
        #endregion
    }
}